package cc.momas.momall.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.momas.common.utils.E3Result;
import cc.momas.common.vo.EasyUITreeNode;
import cc.momas.momall.po.TbContentCategory;
import cc.momas.momall.service.IContentCatService;

@Controller
public class ContentCatController {

	@Autowired
	private IContentCatService conCatSvc;
	
	
	/**
	 * 根据父id查询类目列表,如果没有传过来id就默认为顶级id 0
	 * @param parentId 父id
	 * @return 列表
	 */
	@RequestMapping(value = "/content/category/list")
	@ResponseBody
	public List<EasyUITreeNode> getTree(@RequestParam(value="id",defaultValue="0")Long parentId) {
		List<EasyUITreeNode> tree = conCatSvc.getTree(parentId);
		return tree;
		
	}
	@RequestMapping(value="/content/category/delete/")
	@ResponseBody
	public E3Result deleteContent(Long id) {
		if(id == null) {
			return E3Result.error("id不能为空");
		}
		conCatSvc.delete(id);
		return E3Result.ok();
	}
	
	
	@RequestMapping(value="/content/category/create")
	@ResponseBody
	public E3Result create(TbContentCategory cat) {
		if(cat.getParentId() == null) {
			return E3Result.error("添加失败");
		}
		conCatSvc.save(cat);
		return E3Result.ok();
	}

	@RequestMapping(value="/content/category/update")
	@ResponseBody
	public E3Result update(TbContentCategory cat) {
		int count = conCatSvc.updateByIdSelective(cat);
		return count > 0 ? E3Result.ok() : E3Result.error(null);
	}

}
